package com.wbao.dianniu.ui;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.wbao.dianniu.R;
import com.wbao.dianniu.adapter.TwoCommentAdapter;
import com.wbao.dianniu.adapter.UserInfoHolder;
import com.wbao.dianniu.customView.CanDoBlankGridView;
import com.wbao.dianniu.customView.LoadingWaitDialog;
import com.wbao.dianniu.customView.NoDoubleClickListener;
import com.wbao.dianniu.customView.SpanTextView;
import com.wbao.dianniu.data.AnswerUserInfo;
import com.wbao.dianniu.data.Childrens;
import com.wbao.dianniu.data.QuestAnswerCountsData;
import com.wbao.dianniu.data.QuestAnswerListData;
import com.wbao.dianniu.emojiUtils.EmotionMainFragment;
import com.wbao.dianniu.listener.IAnswerDeleteListener;
import com.wbao.dianniu.listener.IQuestAnswerListListener;
import com.wbao.dianniu.listener.IQuestAnswerListener;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.AnswerDeleteManger;
import com.wbao.dianniu.manager.AnswerPraiseManager;
import com.wbao.dianniu.manager.QAnswerListManger;
import com.wbao.dianniu.manager.QuestAnswerManager;
import com.wbao.dianniu.update.ErrorDialogHelper;
import com.wbao.dianniu.update.TwoComDataChangeManager;
import com.wbao.dianniu.utils.ActivityUtils;
import com.wbao.dianniu.utils.AnswerListType;
import com.wbao.dianniu.utils.CommentLongClickHelper;
import com.wbao.dianniu.utils.JsonUtil;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.ReplyHelper;
import com.wbao.dianniu.utils.Utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 问答 心情 二级回复页面
 * <p>
 * Created by yejianjun on 2018/6/5.
 */

public class TwoCommentQaActivity extends AppCompatActivity implements IQuestAnswerListListener, IQuestAnswerListener, ReplyHelper.ReplyListener, View.OnClickListener
        , CommentLongClickHelper.ITwoCommentListener, IAnswerDeleteListener {

    private LinearLayout waittingLayout;
    private ProgressBar waittingProBar;
    private TextView waittingTV;
    private int REQ_CODE = 600;

    //    private TextView noDataTV;
    private PullToRefreshListView pullListView;
    private ListView mListView;
    private int questId;
    private int pid;
    //    private int commentCounts;
    private QAnswerListManger manger;
    private final int ROWS = 10;
    private int page = 0;
    private TwoCommentAdapter adapter;
    private ImageView aFriendIV;
    private ImageView chosePicIV;
    private CheckBox checkBox;
    private Map<Integer, String> noticeJson = new HashMap<Integer, String>();
    private Map<Integer, String> replyMap = new HashMap<Integer, String>();
    private Dialog pDialog;
    private int type;
    // 问题回复管理类
    private QuestAnswerManager qAnswerManager;
    private InputMethodManager mInputManager;//软键盘管理类
    private AnswerDeleteManger answerDelManger;
    private EmotionMainFragment emotionMainFragment;
    //    private CommentLongClickHelper commentLongClickHelper;
    private EditText bar_edit_text;
    private ImageView bar_image_add_btn;
    private Button bar_btn_send;
    private TextView titleTV;
    private ImageButton backBtn;
    private QuestAnswerListData data = null;

    private UserInfoHolder holder;

    /**
     * 内容
     */
    public SpanTextView contentTV;
    private CanDoBlankGridView mGridView;
    /**
     * 点赞数
     */
    public TextView praiseTV;
    /**
     * 回复数
     */
    public TextView answerTV;
    /**
     * 删除
     */
    private TextView deleteTV;
    /**
     * 转发数
     */
    private TextView forwardTV;
    private View allComment;
    private TextView commentCountsTV;
    //    private LinearLayout emojiBarLayout;
    private boolean IsPullDown = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityUtils.setStatusBarColor(this);
        setContentView(R.layout.activity_two_comment);
        initWattingView();
        initData();
        initView();
        addHeadView();
        initEmotionMainFragment();
        displayHeadView();
    }

    private void initWattingView() {
        waittingLayout = (LinearLayout) findViewById(R.id.waitting_layout);
        waittingProBar = (ProgressBar) findViewById(R.id.waitting_progress);
        waittingTV = (TextView) findViewById(R.id.waitting_text_desc);
    }

    /**
     * 初始化表情面板
     */
    public void initEmotionMainFragment() {
        initEmojiView();
        mInputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        //构建传递参数
        Bundle bundle = new Bundle();
        //绑定主内容编辑框
        bundle.putBoolean(EmotionMainFragment.BIND_TO_EDITTEXT, true);
        //隐藏控件
        bundle.putBoolean(EmotionMainFragment.HIDE_BAR_EDITTEXT_AND_BTN, false);
        //替换fragment
        //创建修改实例
        emotionMainFragment = EmotionMainFragment.newInstance(EmotionMainFragment.class, bundle);
        emotionMainFragment.bindToContentView(pullListView, bar_image_add_btn, bar_edit_text);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        // Replace whatever is in thefragment_container view with this fragment,
        // and add the transaction to the backstack
        transaction.replace(R.id.fl_emotionview_main, emotionMainFragment);
        transaction.addToBackStack(null);
        //提交修改
        transaction.commit();
    }

    private void initEmojiView() {
//        emojiBarLayout = (LinearLayout) findViewById(R.id.include_emoji_bar_layout);
        checkBox = (CheckBox) findViewById(R.id.am_release_checkbox);

        bar_image_add_btn = (ImageView) findViewById(R.id.bar_image_add_btn);
        bar_btn_send = (Button) findViewById(R.id.bar_btn_send);
        bar_btn_send.setOnClickListener(clickListener);
        bar_edit_text = (EditText) findViewById(R.id.bar_edit_text);
//        bar_edit_text.setOnClickListener(this);
        bar_edit_text.setOnEditorActionListener(actionListener);
        bar_edit_text.addTextChangedListener(watcher);
        chosePicIV = (ImageView) findViewById(R.id.bar_image_chose_pic);
        chosePicIV.setClickable(false);
        chosePicIV.setImageResource(R.drawable.no_click_pic);
        aFriendIV = (ImageView) findViewById(R.id.bar_image_a_friends);
        aFriendIV.setOnClickListener(this);

        if (type == AnswerListType.TYPE_QUEST) {
//            if (!GlobalContext.getPerfectData()) {
            checkBox.setVisibility(View.VISIBLE);
            checkBox.setButtonDrawable(R.drawable.xq_checkbox_unpress);
            checkBox.setClickable(false);
//            }
        } else if (type == AnswerListType.TYPE_AM && !GlobalContext.getPerfectData()) {
            checkBox.setVisibility(View.VISIBLE);
            checkBox.setButtonDrawable(R.drawable.xq_checkbox_unpress_ok);
            checkBox.setClickable(false);
            checkBox.setChecked(true);
        }


    }

    private void initView() {

        titleTV = (TextView) findViewById(R.id.title);
        backBtn = (ImageButton) findViewById(R.id.back_button);
        backBtn.setOnClickListener(this);
//        noDataTV = (TextView) findViewById(R.id.no_two_comment_tv);
//        noDataTV.setText("暂无回复");


        pullListView = (PullToRefreshListView) findViewById(R.id.two_comment_listview);
        pullListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullListView.setOnRefreshListener(onRefreshListener2);
        mListView = pullListView.getRefreshableView();
        mListView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_NORMAL);
        registerForContextMenu(mListView);

    }

    private void initData() {
        questId = getIntent().getIntExtra(Const.BUNDLE_QUESTID, 0);
        pid = getIntent().getIntExtra(Const.INTENT_ID, 0);
        type = getIntent().getIntExtra(Const.BUNDLE_ADVERT, 0);
        Bundle bundle = getIntent().getBundleExtra(Const.BUNDLE_DATA);
        data = new QuestAnswerListData();
        if (null != bundle) {
            data.id = bundle.getInt(Const.BUNDLE_ID);
            data.userInfo = new AnswerUserInfo();
            data.userInfo.headPic = bundle.getString(Const.BUNDLE_HEADPIC);
            data.userInfo.accountId = bundle.getInt(Const.BUNDLE_ACCOUNTID);
            data.userInfo.realName = bundle.getString(Const.BUNDLE_REALNAME);
            data.userInfo.label = bundle.getString(Const.BUNDLE_LABEL);
            data.userInfo.sex = bundle.getInt(Const.INTENT_SEX);
            data.userInfo.partner = bundle.getInt(Const.BUNDLE_PARTNER);
            data.userInfo.isIndustryAuthority = bundle.getBoolean(Const.BUNDLE_ISINDUSTRYAUTHORITY);
            data.content = bundle.getString(Const.BUNDLE_CONTENT);
            data.answerImgs = bundle.getString(Const.BUNDLE_QUESTIMGS);
            data.noticeJson = bundle.getString(Const.BUNDLE_NOTICEJSON);
            data.goodCount = bundle.getInt(Const.BUNDLE_GOODCOUNT);
            data.isGood = bundle.getInt(Const.BUNDLE_ISGOOD);
            data.childrensCount = bundle.getInt(Const.BUNDLE_ANSWERCOUNT);
            data.anonymous = bundle.getInt(Const.BUNDLE_ANONYMOUS);
        }

        manger = new QAnswerListManger(this);
        manger.addListener(this);
        qAnswerManager = QuestAnswerManager.getInstance(this);
        qAnswerManager.addQuestAnswerListener(this);
        reqData(page);
        answerDelManger = new AnswerDeleteManger(this);
        answerDelManger.addAnswerDeleteListener(this);

        // 回复@功能监听
        ReplyHelper.getInstance().addReplyListener(this);
        CommentLongClickHelper.getInstance().addTwoCommentListener(this);
    }

    private void addHeadView() {
        View headView = LayoutInflater.from(this).inflate(R.layout.qa_list_item, null);
        holder = new UserInfoHolder();

        holder.headIV = (ImageView) headView.findViewById(R.id.user_head);
        holder.nameTV = (TextView) headView.findViewById(R.id.user_name);
        holder.labelTV = (TextView) headView.findViewById(R.id.user_label);
        holder.sexIV = (ImageView) headView.findViewById(R.id.user_sex);
        holder.partnerIV = (ImageView) headView.findViewById(R.id.user_partner);
        holder.addV = (ImageView) headView.findViewById(R.id.add_v_iv);
        holder.headInfoLayout = (LinearLayout) headView.findViewById(R.id.user_head_layout);
        holder.labelRelative = (RelativeLayout) headView.findViewById(R.id.user_small_relative);
        contentTV = (SpanTextView) headView.findViewById(R.id.qa_content_tv);
        mGridView = (CanDoBlankGridView) headView.findViewById(R.id.noScroollGridView);
        deleteTV = (TextView) headView.findViewById(R.id.include_delete_tv);
        forwardTV = (TextView) headView.findViewById(R.id.include_forward_tv);
        praiseTV = (TextView) headView.findViewById(R.id.include_praise_tv);
        answerTV = (TextView) headView.findViewById(R.id.include_answer_tv);
        contentTV.setMaxLines(30);
        forwardTV.setVisibility(View.GONE);
        deleteTV.setVisibility(View.GONE);

        if (type == AnswerListType.TYPE_AM && data.anonymous == 1) {
            holder.labelRelative.setVisibility(View.GONE);
            holder.sexIV.setVisibility(View.GONE);
            holder.partnerIV.setVisibility(View.GONE);
        } else {
            holder.labelRelative.setVisibility(View.VISIBLE);
            holder.headIV.setOnClickListener(this);
            holder.headInfoLayout.setOnClickListener(this);
        }

        praiseTV.setOnClickListener(this);

        mListView.addHeaderView(headView);
        // 增加二级回复提示 --- 以下是所以二级回复
        allComment = LayoutInflater.from(this).inflate(R.layout.all_comment_layout, null);
        commentCountsTV = (TextView) allComment.findViewById(R.id.all_comment_counts_tv);
        mListView.addHeaderView(allComment);
        adapter = new TwoCommentAdapter(this, type);
        mListView.setAdapter(adapter);
    }

    private void displayHeadView() {
        if (null != data) {
            PictureDownloadUtils.displayHeadImage(this, holder.headIV, data.userInfo.headPic);
            holder.nameTV.setText(data.userInfo.realName == null ? "" : data.userInfo.realName);

            if (1 != data.anonymous) {
                holder.labelTV.setText(data.userInfo.label == null ? "" : data.userInfo.label);
                Utils.showSex(this, data.userInfo.sex, holder.sexIV);
                Utils.showPartner(this, data.userInfo.partner, holder.partnerIV);
                Utils.showAuth(data.userInfo, holder.addV);
            }
//            if (data.userInfo.accountId == GlobalContext.getAccountId()) {
//                deleteTV.setVisibility(View.VISIBLE);
//            } else {
//                deleteTV.setVisibility(View.INVISIBLE);
//            }

            contentTV.setEmojiText(data.content, data.noticeJson);
            contentTV.setOnLongClickListener(longClickListener);
            Utils.showPriseCounts(data.childrensCount, answerTV);
            showCounts(data.childrensCount);
            Utils.praiseChanged(this, data.isGood, data.goodCount, praiseTV);

            if (TextUtils.isEmpty(data.answerImgs)) {
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(10, 5);
                mGridView.setLayoutParams(lp);
                mGridView.setVisibility(View.INVISIBLE);
            } else {
                mGridView.setVisibility(View.VISIBLE);
                Utils.showGridView(this, data.answerImgs, mGridView, false, true);
            }
        }

    }

    View.OnLongClickListener longClickListener = new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            showCopyDialog();
            return true;
        }
    };

    /**
     * 显示复制弹出框
     */
    private void showCopyDialog() {
        CommentLongClickHelper.getInstance().showCopyDialog(this, contentTV.getText().toString());
    }

    private void showCounts(int counts) {
        String str = String.format(getResources().getString(R.string.all_comment_counts), counts + "");
        commentCountsTV.setText(str);
    }

//    PullToRefreshBase.OnRefreshListener onRefreshListener = new PullToRefreshBase.OnRefreshListener() {
//        @Override
//        public void onRefresh() {
//            // 上拉刷新 加载请求数据
//            page++;
//            pullUpRequest();
//        }
//    };

    PullToRefreshBase.OnRefreshListener2 onRefreshListener2 = new PullToRefreshBase.OnRefreshListener2() {
        @Override
        public void onPullDownToRefresh() {
            pullDownRequest();
        }

        @Override
        public void onPullUpToRefresh() {
            // 上拉刷新 加载请求数据
            pullUpRequest();
        }
    };

    // 上拉刷新数据请求
    private void pullUpRequest() {
        IsPullDown = false;
        //没有搜索结果 或者数据为空 上拉没有效果
        page++;
        reqData(page);

    }

    private void pullDownRequest() {
        IsPullDown = true;
        page = 0;
        reqData(page);
    }


    private void reqData(int page) {
        manger.requestAnswerList(GlobalContext.getAccountId(), String.valueOf(questId), page * ROWS, ROWS, pid);
    }

    @Override
    public void onQuestAnswerListFailure(int errorCode, String errorMsg) {
        pullListView.onRefreshComplete(false, false);
        waittingLayout.setVisibility(View.VISIBLE);
        waittingTV.setText(errorMsg);
    }

    @Override
    public void onQuestAnswerListSuccess(List<QuestAnswerListData> objdata, QuestAnswerCountsData qacd) {
        pullListView.onRefreshComplete(false, false);
        waittingLayout.setVisibility(View.GONE);
        if (null != objdata && objdata.size() > 0) {
            if (IsPullDown) {
                adapter.clear();
            }
            adapter.addData(objdata);
            adapter.notifyDataSetChanged();
        }
        updateTitle();
    }

    private void updateTitle() {
        data.childrensCount = adapter.getList().size();
        String title = String.format(getResources().getString(R.string.all_comment_title), adapter.getList().size() + "");
        titleTV.setText(title);
        Utils.showPriseCounts(data.childrensCount, answerTV);
        showCounts(data.childrensCount);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_button:
                finish();
                break;
            case R.id.include_praise_tv:
                if (1 == data.isGood) {
                    data.goodCount--;
                    data.isGood = 0;
                } else {
                    data.goodCount++;
                    data.isGood = 1;
                }
                Utils.praiseChanged(this, data.isGood, data.goodCount, praiseTV);
                //通知后台更新数据
                AnswerPraiseManager manager = new AnswerPraiseManager(this);
                manager.answerPraise(GlobalContext.getAccountId(), data.id);

                // 通知其它界面点赞更新
                TwoComDataChangeManager.getInstance().priseCommentNotify(data.id, data.isGood, data.goodCount);
                break;
            case R.id.bar_image_a_friends:
                Intent aIntent = new Intent();
                aIntent.setClass(TwoCommentQaActivity.this, SelectFriendActivity.class);
                startActivityForResult(aIntent, REQ_CODE);
                break;
            case R.id.user_head_layout:
            case R.id.user_head:
                ActivityUtils.intoHeadInfoActivity(data.userInfo.accountId, TwoCommentQaActivity.this);
                break;
           /* case R.id.bar_edit_text:
                if (!GlobalContext.getPerfectData()) {
                    Notification.toast(this, "您还未完善资料，无法回复");
                }
                break;*/
            default:
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQ_CODE) {
            if (resultCode == RESULT_OK) {
                String userName = data.getStringExtra(Const.INTENT_NICENAME);
                int friendId = data.getIntExtra(Const.BUNDLE_ACCOUNTID, 0);
                if (userName != null && 0 != friendId) {
                    noticeJson.put(friendId, userName);// 加入noticeJson
                    // 光标出插入@
                    int index = bar_edit_text.getSelectionStart();
                    Editable editable = bar_edit_text.getText();
                    editable.insert(index, "@" + userName);
                }
            }
        }

    }

    NoDoubleClickListener clickListener = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            // 未完善资料，不能回复评论
            /*if (!GlobalContext.getPerfectData()) {
                ErrorDialogHelper.getInstance().showPerfectAuth(TwoCommentQaActivity.this);
                return;
            }*/
            String comment = bar_edit_text.getText().toString().trim();
            if (null == comment || "".equals(comment)) {
                Notification.toast(TwoCommentQaActivity.this, "请输入你的评论!");
                return;
            }
            addAnswerComment(comment);
            // 隐藏键盘
            emotionMainFragment.onBackPress();
        }
    };

    @Override
    public void onQuestAnswerFailure(int errorCode, String errorMsg) {
        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }
        Notification.toast(this, errorMsg == null ? "" : errorMsg);
    }

    @Override
    public void onQuestAnswerSuccess(QuestAnswerListData qacd) {
        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }

        // 评论完成 添加一条记录到回复列表
        if (null != qacd) {
            bar_edit_text.setText("");
            bar_edit_text.setHint(getResources().getString(R.string.qa_detail_hint));
            if (null != noticeJson) {
                noticeJson.clear();
            }
            if (null != replyMap) {
                replyMap.clear();
            }
            qacd.accountId = GlobalContext.getAccountId();
            qacd.goodCount = 0;
            adapter.addOneData(qacd);
            adapter.notifyDataSetChanged();
            updateTitle();

            Childrens childrens = new Childrens();
            childrens.id = qacd.id;
            childrens.accountId = qacd.userInfo.accountId;
            childrens.name = qacd.userInfo.realName;
            childrens.content = qacd.content;
            childrens.noticeJson = qacd.noticeJson;
            // 添加一条回答数到一级回复
            TwoComDataChangeManager.getInstance().addOneCommentNotify(qacd.pid, childrens);

            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    // 定位到最上面一条数据，位置发生偏移，需要加上两个addHeadview的内容
                    mListView.setSelection(2);
                }
            });

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ReplyHelper.getInstance().removeReplyListener(this);
        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }
        CommentLongClickHelper.getInstance().removeTwoCommentListener();
        if (null != manger)
            manger.removeListener(this);

        if (null != qAnswerManager) {
            qAnswerManager.removeQuestAnswerListener(this);
        }
        if (null != answerDelManger) {
            answerDelManger.removeAnswerDeleteListener(this);
        }
        // 移除绑定的EditText
        if (null != emotionMainFragment) {
            emotionMainFragment.removeEditText();
        }
    }

    TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.length() == 0) {
                // 清空输入内容时，清除@效果的hashMap
                bar_edit_text.setHint(getResources().getString(R.string.qa_detail_hint));
                if (null != noticeJson) {
                    noticeJson.clear();
                }
                if (null != replyMap) {
                    replyMap.clear();
                }
            }
        }
    };

    TextView.OnEditorActionListener actionListener = new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            // 未完善资料，不能回复评论
            if (!GlobalContext.getPerfectData()) {
                ErrorDialogHelper.getInstance().showPerfectAuth(TwoCommentQaActivity.this);
                return true;
            }
            if (actionId == EditorInfo.IME_ACTION_SEND /*||(event!=null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER)*/) {
                String comment = bar_edit_text.getText().toString().trim();
                if (null == comment || "".equals(comment)) {
                    Notification.toast(TwoCommentQaActivity.this, "请输入你的评论!");
                    return false;
                }
                addAnswerComment(comment);
                // 隐藏键盘
                emotionMainFragment.onBackPress();
                return true;
            }
            return false;
        }
    };

    private void addAnswerComment(String comment) {
        if (null != replyMap && replyMap.size() > 0) {
            for (Map.Entry<Integer, String> entry : replyMap.entrySet()) {
                comment = "回复" + entry.getValue() + ":" + comment;
                noticeJson.put(entry.getKey(), entry.getValue());
            }
        }
        // 二级回复的noticeJson 都要带一级回复的key value
        noticeJson.put(data.userInfo.accountId, data.userInfo.realName);

        // 回复之前 增加等待框
        pDialog = LoadingWaitDialog.createLoadingDialog(this, "");
        pDialog.setCancelable(true);
        pDialog.show();
        int anonymous = 0;
        if (checkBox.isChecked()) {
            anonymous = 1;
        } else {
            anonymous = 0;
        }
        qAnswerManager.addResponse(GlobalContext.getAccountId(), questId, Utils.replaceLineBlanks(comment), JsonUtil.mapToStringJson(noticeJson), null, pid, anonymous);
    }

    @Override
    public void reply(String userName, int userId, int pid) {
        bar_edit_text.setText("");
        bar_edit_text.setHint("回复:" + userName);
        bar_edit_text.requestFocus();
        bar_edit_text.post(new Runnable() {
            @Override
            public void run() {
                mInputManager.showSoftInput(bar_edit_text, 0);
            }
        });
        // add Reply logical
        replyMap.put(userId, userName);
    }

    @Override
    public void onBackPressed() {
        /**
         * 判断是否拦截返回键操作
         */
        if (!emotionMainFragment.isInterceptBackPress()) {
            // 注释这段代码 这个会双击才能退出
            //super.onBackPressed();
            finish();
        }
    }

    /**
     * 获取InputMethodManager，隐藏软键盘
     */
    private void hideKeyboard() {
        emotionMainFragment.onBackPress();
    }


    @Override
    public void onAnswerDeleteFailure(int errorCode, String errorMsg) {

    }

    @Override
    public void onAnswerDeleteSuccess() {

    }

    @Override
    public void twoCommentDel(QuestAnswerListData data) {
        answerDelManger.delete(data.id);
        adapter.deleteById(data.id);
        updateTitle();
        // 通知一级回复界面删除对应二级回复数据
        TwoComDataChangeManager.getInstance().delOneCommentData(pid, data.id);
    }
}
